package com.bolinfest.editor;

import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;

import com.google.appengine.api.users.User;

// From http://code.google.com/appengine/articles/java/retrieving_gdata_feeds.html

public class TokenStore {

  public static String getToken(User user) {
    String id = user.getUserId();
    PersistenceManager pm = PMF.get().getPersistenceManager();

    try {
      Token token = pm.getObjectById(Token.class, id);

      return token.getToken();
    } catch (JDOObjectNotFoundException e) {
      return null;
    } finally {
      pm.close();
    }
  }

  public static void addToken(User user, String sessionToken) {
    String id = user.getUserId();
    PersistenceManager pm = PMF.get().getPersistenceManager();

    try {
      Token token = new Token(id, sessionToken);
      pm.makePersistent(token);
    } finally {
      pm.close();
    }
  }
}